﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Pwp.Module.Channel9.Model;

namespace Pwp.Module.Channel9
{
    public class Channel9RssReader
    {
        private readonly string URL = "http://channel9.msdn.com/Feeds/RSS";

        public IEnumerable<VideoItem> Read()
        {
            var doc = XDocument.Load(URL);
            var items = doc.Descendants("item");

            List<VideoItem> result = new List<VideoItem>();

            foreach (var item in items)
            {
                var title = item.Element("title").Value;
                var link = item.Element("link").Value;

                var mediaformats = item.Element(XName.Get("group", "http://search.yahoo.com/mrss/")).Descendants();

                var channelItem = new VideoItem
                {
                    Title = title,
                    Link = link,
                    Picture = ""
                };

                foreach (var mf in mediaformats)
                {
                    channelItem.Videos.Add(new KeyValuePair<string, string>(mf.Attribute("type").Value, mf.Attribute("url").Value));
                }

                result.Add(channelItem);
            }

            return result;
        }
    }
}
